#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include "MvCameraControl.h"
#include <iostream>
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <opencv2/video/video.hpp>
using namespace std;
using namespace cv;
 void* handle = NULL;
bool g_bExit = false;
unsigned int g_nPayloadSize = 0;
unsigned char *pDataForRGB  = (unsigned char*)malloc(1280 *960 * 4 + 2048);






bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
{
    if (NULL == pstMVDevInfo)
    {
        printf("The Pointer of pstMVDevInfo is NULL!\n");
        return false;
    }
    if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE)
    {
        int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
        int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
        int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
        int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
        // ch:打印当前相机ip和用户自定义名字 | en:print current ip and user defined name
        printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
        printf("UserDefinedName: %s\n\n" , pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
    }
    else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE)
    {
        printf("UserDefinedName: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
        printf("Serial Number: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chSerialNumber);
        printf("Device Number: %d\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.nDeviceNumber);
    }
    else
    {
        printf("Not support.\n");
    }
    return true;
}
void close_cam(void)
{
        int nRet = MV_CC_StopGrabbing(handle);
        if (MV_OK == nRet)
        {
            printf("Stopped Grabbing ! \n");

        }


}
void start_cam(void)
{
        int nRet = MV_OK;

     MV_CC_DEVICE_INFO_LIST stDeviceList;
        memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
        nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, &stDeviceList);
        if (MV_OK != nRet)
        {
            printf("Enum Devices fail! nRet [0x%x]\n", nRet);
        }using namespace std;
        if (stDeviceList.nDeviceNum > 0)
        {
            for (unsigned int i = 0; i < stDeviceList.nDeviceNum; i++)
            {
                //printf("[device %d]:\n", i);
                MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
                if (NULL == pDeviceInfo)
                {
                    break;
                }
                PrintDeviceInfo(pDeviceInfo);
            }
        }
        else
        {
            printf("Find No Devices!\n");
        }
        unsigned int nIndex = 0;
        //scanf("%d", &nIndex);
        if (nIndex >= stDeviceList.nDeviceNum)
        {
            printf("Intput error!\n");
        }
        // ch:选择设备并创建句柄 | en:Select device and create handle
        nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
        if (MV_OK != nRet)
        {
            printf("Create Handle fail! nRet [0x%x]\n", nRet);
        }
        // ch:打开设备 | en:Open device
        nRet = MV_CC_OpenDevice(handle);
        if (MV_OK != nRet)
        {
            printf("Open Device fail! nRet [0x%x]\n", nRet);
        }
        // ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
        if (stDeviceList.pDeviceInfo[nIndex]->nTLayerType == MV_GIGE_DEVICE)
        {
            int nPacketSize = MV_CC_GetOptimalPacketSize(handle);
            if (nPacketSize > 0)
            {
                nRet = MV_CC_SetIntValue(handle,"GevSCPSPacketSize",nPacketSize);
            }
            else
            {
                printf("Warning: Get Packet Size fail nRet [0x%x]!", nPacketSize);
            }
        }
        MVCC_ENUMVALUE  p={0};
        MVCC_STRINGVALUE st;
        MV_CC_GetStringValue(handle,"DeviceModelName",&st);
        cout<<"DeviceModelName: "<<st.chCurValue<<endl;
        MV_CC_GetStringValue(handle,"DeviceVersion",&st);
        cout<<"DeviceVersion:\t"<<st.chCurValue<<endl;
        MV_CC_GetEnumValue(handle,"DeviceScanType",&p);
        if(p.nCurValue==0){
        cout<<"DeviceScanType:\t"<<"Areascan"<<endl;}
        else{
        cout<<"DeviceScanType:\t"<<"Linescan"<<endl;}
        // ch:设置触发模式为off | en:Set trigger mode as off
        nRet = MV_CC_SetEnumValue(handle, "TriggerMode", 0);
        if (MV_OK != nRet)
        {
            printf("Set Trigger Mode fail!Frame nRet [0x%x]\n", nRet);
        }
        nRet = MV_CC_SetEnumValue(handle, "PixelFormat", 0x0210001F);
        if (MV_OK != nRet)
        {
            printf("Set PixelFormat fail!Frame nRet [0x%x]\n", nRet);
        }
        nRet = MV_CC_SetEnumValue(handle, "GainAuto", 1);
        if (MV_OK != nRet)
        {
            printf("Set GainAuto fail!Frame nRet [0x%x]\n", nRet);
        }
        nRet = MV_CC_SetFloatValue(handle, "Gamma", 0.8);
        if (MV_OK != nRet)
        {
            printf("Set Gamma fail!Frame nRet [0x%x]\n", nRet);
        }
        nRet = MV_CC_SetBoolValue(handle, "GammaEnable", 1);
        if (MV_OK != nRet)
        {
            printf("Set GammaEnable fail!Frame nRet [0x%x]\n", nRet);
        }
        nRet = MV_CC_SetEnumValue(handle, "BalanceWhiteAuto", 2);
        if (MV_OK != nRet)
        {
            printf("Set BalanceWhiteAuto fail!Frame nRet [0x%x]\n", nRet);
        }
        nRet = MV_CC_SetEnumValue(handle, "ExposureAuto", 1);
        if (MV_OK != nRet)
        {
            printf("Set ExposureAuto fail!Frame nRet [0x%x]\n", nRet);
        }
        // ch:获取数据包大小 | en:Get payload size
        MVCC_INTVALUE stParam;
        memset(&stParam, 0, sizeof(MVCC_INTVALUE));
        nRet = MV_CC_GetIntValue(handle, "PayloadSize", &stParam);
        if (MV_OK != nRet)
        {
            printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
        }
        g_nPayloadSize = stParam.nCurValue;
        // ch:开始取流 | en:Start grab image
        nRet = MV_CC_StartGrabbing(handle);
        if (MV_OK == nRet)
        {
            printf("Start Grabbing ! \n");

        }

        nRet = MV_CC_SetEnumValue(handle, "BalanceWhiteAuto", 2);
        if (MV_OK != nRet)
        {
            printf("Set BalanceWhiteAuto fail!Frame nRet [0x%x]\n", nRet);
        }
        nRet = MV_CC_SetEnumValue(handle, "ExposureAuto", 1);
        if (MV_OK != nRet)
        {
            printf("Set ExposureAuto fail!Frame nRet [0x%x]\n", nRet);
        }

        cout<<"\nPress ESC to exit.\n";

        //return 0;
}
void get_pic(Mat *srcimg){

        MV_CC_PIXEL_CONVERT_PARAM CvtParam={0};
        MV_FRAME_OUT stOutFrame = {0};
        memset(&stOutFrame, 0, sizeof(MV_FRAME_OUT));
        int nRet = MV_CC_GetImageBuffer(handle, &stOutFrame, 400);

        CvtParam.enSrcPixelType=stOutFrame.stFrameInfo.enPixelType;
        CvtParam.enDstPixelType=PixelType_Gvsp_RGB8_Packed;
        CvtParam.nHeight=stOutFrame.stFrameInfo.nHeight;
        CvtParam.nWidth=stOutFrame.stFrameInfo.nWidth;
        CvtParam.nDstBufferSize=stOutFrame.stFrameInfo.nWidth * stOutFrame.stFrameInfo.nHeight *  4 + 2048;
        CvtParam.pSrcData=stOutFrame.pBufAddr;
        CvtParam.pDstBuffer=pDataForRGB;
        CvtParam.nSrcDataLen=stOutFrame.stFrameInfo.nFrameLen;
        nRet =MV_CC_ConvertPixelType(handle,&CvtParam);
        *srcimg=Mat(stOutFrame.stFrameInfo.nHeight,stOutFrame.stFrameInfo.nWidth,CV_8UC3,pDataForRGB);
        cvtColor(*srcimg,*srcimg,COLOR_RGB2BGR);
            //cout<<"out:"<<out_frame->stFrameInfo.nHeight<<endl;
        if(NULL != stOutFrame.pBufAddr)
            {
                nRet = MV_CC_FreeImageBuffer(handle, &stOutFrame);
            }

}
void set_cam_para(){
    //cout<<"set para"<<endl;
    {
    MV_CC_SetEnumValue(handle, "BalanceWhiteAuto", 2);
    MV_CC_SetEnumValue(handle, "ExposureAuto", 1);
    }

}
